package com.example.littleken.secret.Activitys;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.littleken.secret.Config;
import com.example.littleken.secret.Id.MyContacts;
import com.example.littleken.secret.Net.Message;
import com.example.littleken.secret.Net.Timeline;
import com.example.littleken.secret.Net.UploadContacs;
import com.example.littleken.secret.R;
import com.example.littleken.secret.tools.MD5Tool;

import java.util.List;

/**
 * 本类实现了主页面信息显示的具体逻辑
 */
public class AtyTimeline extends ListActivity {
    private String phone_num;
    private String phone_md5;
    private String token;
    private AtyTimelineMessageListAdapter adapter = null;
    private ListView listView;
    private Button btn_publish;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aty_timeline);
        //listView = (ListView) findViewById(android.);
        //实例化适配器
        adapter = new AtyTimelineMessageListAdapter(this);
        //设置适配器
        setListAdapter(adapter);
        //实现消息发送
        findViewById(R.id.btn_publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AtyTimeline.this,AtyPublish.class);
                intent.putExtra(Config.KEY_PHONE_MD5,phone_md5);
                intent.putExtra(Config.KEY_TOKEN,token);
                startActivityForResult(intent,Config.REQUESTCODE);
            }
        });
        //数据初始化
        phone_num = getIntent().getStringExtra(Config.KEY_PHONE_NUM);
        token = getIntent().getStringExtra(Config.KEY_TOKEN);
        phone_md5 = MD5Tool.md5(phone_num);

        final ProgressDialog pd = ProgressDialog.show(this,getResources().getString(R.string.connecting),getResources().getString(R.string.connecting_to_server));

        new UploadContacs(phone_md5, token, MyContacts.getContactsJsonString(this), new UploadContacs.SuccessCallback() {
            @Override
            public void onSuccess() {
                loadMessage();
                pd.dismiss();
            }
        }, new UploadContacs.FailCallback() {
            @Override
            public void onFail(int errorCode) {
                pd.dismiss();
                if(errorCode == Config.RESULT_STATUS_INVALID_TOKEN){
                    startActivity(new Intent(AtyTimeline.this,AtyLogin.class));
                    finish();
                }else {
                    loadMessage();
                }
            }
        });
    }

    /**
     * 本方法实现了联机服务器并加载数据
     */
    private void loadMessage(){
        Log.e("TAG",">>>>>>LoadMessage");
        final ProgressDialog pd = ProgressDialog.show(this,getResources().getString(R.string.connecting),getResources().getString(R.string.connecting_to_server));
        new Timeline(phone_md5, token, 1, 20, new Timeline.SuccessCallback() {
            @Override
            public void onSuccess(int page, int perpage, List<Message> timeline) {
                pd.dismiss();
                adapter.clear();
                adapter.addAll(timeline);
            }
        }, new Timeline.FailCallback() {
            @Override
            public void onFail(int errorCode) {
                pd.dismiss();
                if(errorCode == Config.RESULT_STATUS_INVALID_TOKEN){
                    startActivity(new Intent(AtyTimeline.this,AtyLogin.class));
                    finish();
                }else{
                    Toast.makeText(AtyTimeline.this,R.string.fail_to_load_timeline_data,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    Message msg ;
    @Override
    //实现接口，实现每个信息在点击之后进去其具体消息页面
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        msg = adapter.getItem(position);
        Intent intent = new Intent(this,AtyMessage.class);
        intent.putExtra(Config.KEY_MSG,msg.getMsg());
        intent.putExtra(Config.KEY_MSG_ID,msg.getMseId());
        intent.putExtra(Config.KEY_PHONE_MD5,msg.getPhone_md5());
        intent.putExtra(Config.KEY_TOKEN,token);
        startActivity(intent);
    }

    @Override
    //本方法处理activity返回消息，刷新页面
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case Config.ACTIVITY_NEED_RESULT_NEED_REFRESH:
                loadMessage();
                break;
            default:
                break;
        }
    }
}
